package com.jsgt.greekfire.viewmodel

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.fhx.core.base.BaseViewModel
import com.jsgt.greekfire.bean.CurrencyDescribe
import com.jsgt.greekfire.bean.DealsBean
import com.jsgt.greekfire.bean.DepthBean
import com.jsgt.greekfire.common.http.safeLaunch
import com.jsgt.greekfire.repository.KLineRepository

class KLineViewModel() : BaseViewModel() {
    private val kLineRepository = KLineRepository.instance

    val errorLiveData by lazy { MutableLiveData<Any>() }
    val kLineDataLiveData by lazy { MutableLiveData<List<List<String>>>() }
    val depthLiveData by lazy { MutableLiveData<DepthBean>() }
    val dealsLiveData by lazy { MutableLiveData<List<DealsBean>>() }
    val currencyDescribeLiveData by lazy { MutableLiveData<CurrencyDescribe>() }

    /**
     * 获取k线数据
     */
    fun getKLineData(type: String, granularity: Int) {
        viewModelScope.safeLaunch {
            block = {
                val datas = kLineRepository.getKLineData(type, granularity)
                if (datas.code == 0) {
                    kLineDataLiveData.value = datas.data
                } else {
                    errorLiveData.value = datas.msg
                }
            }
            onError = {
                errorLiveData.value = it.message
            }
        }
    }

    /**
     * 获取深度
     */
    fun getDepth(type: String) {
        viewModelScope.safeLaunch {
            block = {
                val responseData = kLineRepository.getDepth(type)
                if (responseData.code == 0) {
                    depthLiveData.value = responseData.data
                } else {
                    errorLiveData.value = responseData.msg
                }
            }
            onError = {
                errorLiveData.value = it.message;
            }
        }
    }


    /**
     * 获取成交
     */
    fun getDeals(type: String) {
        viewModelScope.safeLaunch {
            block = {
                val responseData = kLineRepository.getDeals(type)
                if (responseData.code == 0) {
                    dealsLiveData.value = responseData.data
                } else {
                    errorLiveData.value = responseData.msg
                }
            }
            onError = {
                errorLiveData.value = it.message;
            }
        }
    }


    /**
     * 获取简介
     */
    fun getCurrencyDescribe(type: String) {
        viewModelScope.safeLaunch {
            block = {
                val responseData = kLineRepository.getCurrencyDescribe(type)
                if (responseData.code == 0) {
                    currencyDescribeLiveData.value = responseData.data
                } else {
                    errorLiveData.value = responseData.msg
                }
            }
            onError = {
                errorLiveData.value = it.message;
            }
        }
    }
}